package com.cqjtu.email;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * Created by Administrator on 2016/11/13 0013.
 */
public class checkState {

    public static boolean Check() {
        if (!MainActivity.username.equals("") && !MainActivity.password.equals("") && MainActivity.username != null
                && MainActivity.password != null) {
            int POP3port = 110;
            Socket client = null;
            try {
                //向pop3服务程序建立一个套接字连接
                client = new Socket("pop3.163.com", POP3port);
                //读取输出
                InputStream is = client.getInputStream();
                BufferedReader sockin = new BufferedReader(new InputStreamReader(is));
                //创建一个PrintWriter对象，以便向套接字写入内容
                OutputStream os = client.getOutputStream();
                PrintWriter sockout = new PrintWriter(os, true);
                //显示同SMTP服务程序的握手过程
                sockin.readLine();
                sockout.println("user " + MainActivity.username);
                sockin.readLine();
                sockout.println("pass " + MainActivity.password);
                String str = sockin.readLine();
                if(str.contains("message")){
                    if (client != null) {
                        client.close();
                    }
                    return true;
                }
                return false;

            } catch (IOException e) {
                System.out.println(e.toString());
            } finally {
                try {
                    if (client != null) {
                        client.close();
                    }
                } catch (IOException e) {
                }
            }

        }
        return false;
    }
}
